package com.example.exercise;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Looper;
import android.widget.Toast;

import com.example.exercise.MysqlUtil.CheckMysqlConnect;
import com.example.exercise.MysqlUtil.DBService;
import com.example.exercise.MysqlUtil.Mysqlconnect;
import com.example.exercise.Utils.GetNetWorkTime;

import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class WelCome extends AppCompatActivity {

    ProgressDialog waitingDialog;
    String message;
    String hangzhou = "连接成功", hanguo = "连接成功";
    SharedPreferences sharedPreferences ;
    SharedPreferences.Editor editor;


    @SuppressLint("WrongConstant")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wel_come);
        showWaitingDialog();
        checkmysql();




    }

    private void checkmysql() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                sharedPreferences = getSharedPreferences("state",MODE_PRIVATE);
                editor = sharedPreferences.edit();
                if (CheckMysqlConnect.getMainMysqlstate() == 1){

                    Looper.prepare();
                    editor.putInt("state",1);
                    editor.apply();
                    showState();
                    Looper.loop();

                }else{

                    Looper.prepare();
                   // Toast.makeText(getApplicationContext(),"连接杭州数据中心失败",Toast.LENGTH_SHORT).show();
                    hangzhou ="连接失败";
                    Mysqlconnect.get().url = Mysqlconnect.get().url1;
                    editor.putInt("state",0);
                    editor.apply();
                    System.out.println("连接失败");
                    showState();
                    Looper.loop();


                }
            }
        }).start();



    }


    private void showTimeCurrect(){

        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(WelCome.this);
        normalDialog.setTitle("时间不正确");
        normalDialog.setMessage("本机时间和网络时间有较大差别，无法进入软件，请调整时间后再进入");
        normalDialog.setPositiveButton("知道了",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        System.exit(0);
                        overridePendingTransition(0, 0);

                    }
                });

        // 显示
        normalDialog.show();

    }
    private void showupdate() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                Double version = 1.10;
                try{
                    if (Double.valueOf(DBService.getVersion()) > version){
                        Looper.prepare();
                        waitingDialog.dismiss();
                        showUpdateDialog();
                        Looper.loop();

                    }else{

                        Looper.prepare();
                        waitingDialog.dismiss();
                        if (DBService.getMessage().trim().equals("")){


                            showDialog("暂无公告");
                        }else {
                            if (GetNetWorkTime.getNetTime() == 1){
                                System.out.println(GetNetWorkTime.getNetHour()+"9999999999999");
                                showDialog(DBService.getMessage());
                            }else {
                                showTimeCurrect();
                            }
                        }

                        Looper.loop();



                    }
                }catch (Exception e){
                    e.printStackTrace();
                    Looper.prepare();
                    Toast.makeText(getApplicationContext(),"网络未连接，无法获取更新",Toast.LENGTH_LONG).show();
                    waitingDialog.setMessage("网络未连接，无法获取更新");
                    Looper.loop();
                }

            }
        }).start();
    }

    private void showState() {

        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(WelCome.this);
        normalDialog.setTitle("数据中心连接状态");
        normalDialog.setMessage("杭州数据中心:"+hangzhou+"\n韩国数据中心:"+hanguo);
        normalDialog.setPositiveButton("知道了",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        showupdate();

                    }
                });

        // 显示
        normalDialog.show();
    }

    private void showDialog(String message) {

        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(WelCome.this);
        normalDialog.setTitle("公告");
        normalDialog.setMessage(message);
        normalDialog.setPositiveButton("知道了",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(getApplicationContext(),Auth.class);
                        startActivity(intent);
                        Toast.makeText(getApplicationContext(),"欢迎进入本软件",Toast.LENGTH_SHORT).show();


                    }
                });

        // 显示
        normalDialog.show();
    }

    private void showUpdateDialog() {

        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(WelCome.this);
        normalDialog.setTitle("更新提示");
        normalDialog.setMessage("该软件存在更新版本，请访问https://wwn.lanzoul.com/b02oeqkna\n" +
                "密码6666 \n下载最新正式版");
        normalDialog.setPositiveButton("点我去更新",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Uri uri = Uri.parse("https://wwn.lanzoul.com/b02oeqkna");
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        startActivity(intent);
                        System.exit(0);
                        overridePendingTransition(0, 0);
                    }
                });
        normalDialog.setNegativeButton("退出软件",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        System.exit(0);
                        overridePendingTransition(0, 0);

                    }
                });
        // 显示
        normalDialog.show();
    }

    private void showWaitingDialog() {
        /* 等待Dialog具有屏蔽其他控件的交互能力
         * @setCancelable 为使屏幕不可点击，设置为不可取消(false)
         * 下载等事件完成后，主动调用函数关闭该Dialog
         */
        waitingDialog=
                new ProgressDialog(WelCome.this);
        waitingDialog.setTitle("正在检查更新");
        waitingDialog.setMessage("检查中，请务必联网");
        waitingDialog.setIndeterminate(true);
        waitingDialog.setCancelable(false);
        waitingDialog.show();
    }
}